// routes/publicKey.js
import Router from '@koa/router';
import { getRegPublicKey } from '../utils/crypto.js';

const router = new Router();

router.get('/api/public-key', async (ctx) => {
  const publicKey = await getRegPublicKey();
  try {

    ctx.set('Cache-Control', 'max-age=300'); // 缓存5分钟
    ctx.body = {
      code: 200,
      data: {
        publicKey: publicKey,
        algorithm: 'RSA-OAEP', // 告知前端使用的加密算法
        keyId: process.env.KEY_ID // 可选：密钥版本标识
      }
    };
  } catch (err) {
    ctx.throw(500, '公钥获取失败');
  }
});

export default router;